package com.aolong.archives.common.resource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.aolong.archives.common.util.EntityUtil;
import com.aolong.archives.common.util.Const;
import com.rock.platform.util.SessionUtils;

public class ExceptionDictionaryUtil {

	private static final ExceptionDictionaryUtil _instance = new ExceptionDictionaryUtil();    
	
	//Map<tableName,Map<constraintName,Map<language,value>>>
    private Map<String,Map<String,String[]>> lookup = new HashMap<String,Map<String,String[]>>() ;
    
	public static ExceptionDictionaryUtil getInstance() {    
	    return _instance;    
	}    
	
	private ExceptionDictionaryUtil(){
	}
	
	/**
	 * 加载异常信息资源
	 * @param em
	 */
	public void loadResource(EntityManager em)
	{
		Query query = em.createNativeQuery("select tableName,constraintName,value,valueEn from TSM_ExceptionDictionary");
		List<?> result = query.getResultList();
        this.addResource(result);
	}
	
	private void addResource (List<?> list) 
	{
        if (list == null) return;

        for(int i = 0; i < list.size(); i++)
		{
			Object[] objAry = (Object[])list.get(i);

			Map<String,String[]> tableMap = lookup.get(objAry[0].toString());
			if (tableMap == null)
			{
				tableMap = new HashMap<String,String[]>();
				
			}
			String[] values = {objAry[2].toString(),objAry[3].toString()};
			tableMap.put(objAry[1].toString(),values);
		}
    }
	
	/**
	 * 清空内存中的异常信息资源
	 */
	public void clearResource()
	{
		lookup.clear();
	}
	
	private Map<String,String[]> getLookup(String tableName)
	{
		Map<String,String[]> tableMap = lookup.get(tableName);
		return (tableMap == null) ? new HashMap<String,String[]>() : tableMap;
	}

	/**
	 * 根据表名、约束名 获取异常信息中国际化信息， 自动识别session中语言信息  
	 * @param tableName
	 * @param constraintName
	 * @return
	 */
	public String getValue(String tableName,String constraintName) {
        
		return getValue(tableName,constraintName,SessionUtils.getAttributeText(SessionUtils.SKEY_LANGUAGE));
	}
	
	/**
	 * 根据表名、约束名、语言  获取异常信息中国际化信息
	 * @param tableName
	 * @param constraintName
	 * @param language
	 * @return
	 */
	public String getValue(String tableName,String constraintName,String language) {
        
		if (EntityUtil.isNullOrEmpty(constraintName) || EntityUtil.isNullOrEmpty(tableName) ) {
            return "";
        }
		
        String[] values = getLookup(tableName.toLowerCase()).get(constraintName.toLowerCase());
        if (values == null)
        {
        	return "";
        }
        else
        {
        	return (language.equals(Const.LANGUAGE_en_US)) ? values[1] : values[0];
        }
	}

	/**
	 * 解析数据库抛出的异常信息，获取异常字典中的国际化信息
	 * @param exMsg
	 * @return
	 */
	public static String parse(String exMsg)
	{
		String tableName = null;
		String constraintName= null;

		//TODO parse exMsg 
		
		String value = ExceptionDictionaryUtil.getInstance().getValue(tableName,constraintName);
		return EntityUtil.isNullOrEmpty(value) ? exMsg : value;
	}
}
